
import * as PARSE from '../src/system/render/shaderlib-parse'


const SHADERLIB_SOURCE = `
Shader "My Wgsl Shader" {
    Pass {
        Name "My Pass"

        #define X

        Cull front
        
        Blend src-alpha one-minus-src-alpha
        BlendOp add
        
        ZWrite off

        WGSLPROGRAM

        struct Uniform {
            ofs: f32,
        };
        
        #ifndef X

        struct Mats {
            MVP: mat4x4f,
            M: mat4x4f
        }
        
        struct vin {
            @location(0) pos: vec4f,
            @location(1) uv: vec2f, 
            @location(2) normal: vec4f,
        }

        #else
        
        struct v2f {
            @builtin(position) pos: vec4f,
            @location(1) uv: vec2f,
            @location(2) lambert: f32,
            @location(3) normal: vec3f
        }
        
        #endif
        
        @group(0) @binding(1) var mainTex: texture_2d<f32>;
        @group(0) @binding(2) var mainTexSampler: sampler;
        @group(0) @binding(3) var<uniform> mats: Mats;
        
        @vertex
        fn vert(in: vin) -> v2f {
            var fakeNorm = normalize(in.pos);
        
            var out: v2f;
            out.pos = mats.MVP * (in.pos);
            out.uv = in.uv;
            out.lambert = dot(normalize((mats.M * in.normal).xyz), normalize(vec3f(-1, -1, -1)));
            out.normal = normalize((mats.M * in.normal).xyz);
        
            return out;
        }
        
        @fragment
        fn frag(fin: v2f) -> @location(0) vec4f {
            return textureSample(mainTex, mainTexSampler, fin.uv) * saturate(fin.lambert * 0.5 + 0.5 + 0.3);
        }

        ENGWGSL
    }
}
`

const result = PARSE.parseShaderLib(SHADERLIB_SOURCE, '<source>')
// console.log('result: ' + JSON.stringify(result, null, ' '))

console.log('generated code: \n' + result!.shaderBody.passes[0].wgslProgram)